import "package:share_image_server/export_all.dart";


//
// auth: Du Long
// date: 2024-09-15
// description:
//

Future<Response> uploadService(Request request) async {
  ResponseApi responseApi = ResponseApi(code: 200, msg: "成功", dataType: "Array", data: []);
  List<Map<String, String>> result = [];
  if (request.formData() case var form?) {
    await for (final formData in form.formData) {
      if (formData.filename == null || !formData.part.headers.containsKey("content-type")) {
        continue;
      }
      final contentType = formData.part.headers["content-type"];
      if (!allowedFileTypes.containsKey(contentType)) {
        continue;
      }
      final filename = formData.filename.toString();
      final uuidFile = "${Uuid().v4()}.${allowedFileTypes[contentType]}";
      final filePath = "$imagePath/$uuidFile";
      final file = File(filePath);
      await file.writeAsBytes(await formData.part.readBytes());
      Map<String, String> form = {
        "filename": filename,
        "uuid_file": uuidFile,
      };
      result.add(form);
    }
    responseApi.data = result;
  }
  return Response(responseApi.code, body: responseApiToJson(responseApi))
      .change(headers: {"content-type": "application/json"});
}
